package org.scau.studentevaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.scau.studentevaluation.entity.bo.AttendedStudentBO;
import org.scau.studentevaluation.entity.dos.AttendedStudentListDO;
import org.scau.studentevaluation.entity.query.ActExistQuery;
import org.scau.studentevaluation.entity.query.AttendedStudentListQuery;
import org.scau.studentevaluation.entity.vo.AttendedStudentVO;
import org.scau.studentevaluation.service.IAttendedStudentListService;
import org.scau.studentevaluation.service.ImportMapperService;
import org.scau.studentevaluation.util.BeanConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AttendedStudentListServiceImpl implements IAttendedStudentListService {

    @Autowired
    private ImportMapperService importMapperService;

    @Override
    public List<AttendedStudentVO> getActStudentsByActId(Long actId) {
        LambdaQueryWrapper<AttendedStudentListDO> query = Wrappers.<AttendedStudentListDO>lambdaQuery()
                .eq(AttendedStudentListDO::getActId, actId);
        List<AttendedStudentListDO> DOs = importMapperService.attendedStudentListMapper.selectList(query);

        List<AttendedStudentBO> BOs = BeanConvertUtils.DOs2BOs(DOs,AttendedStudentBO.class);
        List<AttendedStudentVO> VOs = BeanConvertUtils.BOs2VOs(BOs,AttendedStudentVO.class);

        return VOs;
    }

    @Override
    @Transactional
    public int deleteStudentsByActId(Long actId) {
        LambdaQueryWrapper<AttendedStudentListDO> query = Wrappers.<AttendedStudentListDO>lambdaQuery()
                .eq(AttendedStudentListDO::getActId, actId);
        return importMapperService.attendedStudentListMapper.delete(query);
    }

    @Override
    public List<Long> getActIdByStudentId(Long studentId) {
        LambdaQueryWrapper<AttendedStudentListDO> query = Wrappers.<AttendedStudentListDO>lambdaQuery()
                .eq(AttendedStudentListDO::getStudentId, studentId);
        List<AttendedStudentListDO> DO = importMapperService.attendedStudentListMapper.selectList(query);
        List<Long> actIds = new ArrayList<Long>();
        for (AttendedStudentListDO attendedStudentListDO : DO){
            actIds.add(attendedStudentListDO.getActId());
        }
        return actIds;
    }

    @Override
    public List<Long> getActIdByCollege(String college) {
        LambdaQueryWrapper<AttendedStudentListDO> query = Wrappers.<AttendedStudentListDO>lambdaQuery()
                .eq(AttendedStudentListDO::getCollege, college);
        List<AttendedStudentListDO> DO = importMapperService.attendedStudentListMapper.selectList(query);
        List<Long> actIds = new ArrayList<Long>();
        for (AttendedStudentListDO attendedStudentListDO : DO){
            actIds.add(attendedStudentListDO.getActId());
        }
        return actIds;
    }

    @Override
    public int actExist(ActExistQuery actExistQuery) {
        LambdaQueryWrapper<AttendedStudentListDO> query = Wrappers.<AttendedStudentListDO>lambdaQuery()
                .eq(AttendedStudentListDO::getStudentId, actExistQuery.getStudentId())
                .eq(AttendedStudentListDO::getActId,actExistQuery.getActId())
                .last("limit 1");
        AttendedStudentListDO DO = importMapperService.attendedStudentListMapper.selectOne(query);
        if (DO == null){
            return 0;
        }
        return 1;
    }

    @Override
    public int InsertAttendedStudent(List<AttendedStudentListQuery> queryList) {
        queryList.forEach(student -> {
            AttendedStudentListDO DO = new AttendedStudentListDO();
            BeanUtils.copyProperties(student, DO);
            importMapperService.attendedStudentListMapper.insert(DO);
        });
        return 0;
    }
}
